//
//  PPGenderSettingViewController.m
//  pickpic
//
//  Created by Jinxiao on 9/16/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPGenderSettingViewController.h"

#import "PPNetworkService.h"

@interface PPGenderSettingViewController ()
@property (readwrite, nonatomic, assign) PPUserGender gender;
@property (weak, nonatomic) IBOutlet UIView *bottomLine;
@end

@implementation PPGenderSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"性别";
    self.tableView.bounces = NO;
    self.view.backgroundColor = WHITE;
    
    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
    _gender = profiles.gender.integerValue;

    __weak typeof(self) weakSelf = self;
    [self setNavigationLeftTitle:@"取消" titleColor:[UIColor colorWithRGBHex:0xf2f2f2] action:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];

    [self setNavigationRightTitle:@"保存" titleColor:[UIColor colorWithRGBHex:0xffdd17] action:^{
        [weakSelf saveItemDidPressed];
    }];


    
    if (IS_IOS_8) {
            [self.navigationItem.leftBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:PFFontMedium(16)} forState:UIControlStateNormal];
    }else{
    [self.navigationItem.leftBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:PPFONT(PFBold, 16)} forState:UIControlStateNormal];
    }
    
    
    self.navigationItem.leftBarButtonItem.tintColor = BLACK;

    
    
    if (IS_IOS_8) {
            [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:PFFontMedium(16)} forState:UIControlStateNormal];
    }else{
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:PPFONT(PFBold, 16)} forState:UIControlStateNormal];
    }
    
    self.navigationItem.rightBarButtonItem.tintColor = COLORA(0, 118, 255);
    
    [self layoutNavigationBar];
    
     [self.bottomLine addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    
}

- (void)layoutNavigationBar {
    UIBarButtonItem *confirmItem = self.navigationItem.rightBarButtonItem;

    if(_gender != PPUserGenderUnknown) {
        confirmItem.enabled = YES;
//        confirmItem.tintColor = [UIColor colorWithRGBHex:0xffdd17];
    } else {
        confirmItem.enabled = NO;
//        confirmItem.tintColor = [UIColor colorWithRGBHex:0xb2b2b2];
    }
}

- (void)saveItemDidPressed {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
    PPUserID userID = profiles.userID;

    __weak typeof(self) weakSelf = self;
    [PPNetworkService updateProfileWithName:nil gender:@(_gender) country:nil state:nil city:nil birthday:nil signature:nil type:2 callback:^(id data, NSError *error) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if(strongSelf == nil)
        {
            return;
        }

        [hud hide:YES];

        if(error == nil) {
            PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;

            if([profiles.userID isEqualToString:userID]) {
                profiles.gender = @(strongSelf.gender);
                [profiles.managedObjectContext saveContext];
            }

            [strongSelf.navigationController.view toastMessage:@"变更成功" duration:1.f completion:^{
                [strongSelf.navigationController popViewControllerAnimated:YES];
            }];

        } else {
            [strongSelf.navigationController.view toastMessage:error.networkPrompt];
        }
    }];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    PPUserGender gender = PPUserGenderUnknown;

    if(indexPath.row == 0) {
        gender = PPUserGenderMale;
    } else {
        gender = PPUserGenderFemale;
    }

    if(_gender == gender) {
        UIImageView *indicatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Check"]];
        cell.accessoryView = indicatorView;
    } else {
        cell.accessoryView = nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PPUserGender gender = PPUserGenderUnknown;

    if(indexPath.row == 0) {
        gender = PPUserGenderMale;
    } else {
        gender = PPUserGenderFemale;
    }

    _gender = gender;

    [tableView reloadData];

    [self layoutNavigationBar];
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 15;
//}
@end
